#include "vecDirFiles.h"
#include <iostream>

vecDirFiles::vecDirFiles( std::string directory ): hFind( INVALID_HANDLE_VALUE ), Dir((LPTSTR) malloc (MAX_PATH) ), directory( directory )
{
	setDir( directory );
}

vecDirFiles::~vecDirFiles()
{
	FindClose(hFind);
	free(Dir);
}

// gets the current working directory and adds CWDDir to 
// it for accessing the subdirector of the CWD file.
//******************************
// THINK ABOUT MODIFING TO PASS A DIRECTORY TO ADD TO THE CURRENT ONE
// i.e. pass string directory 'Textures' adds /Textures
// i.e. pass string directory 'map' adds /map
std::string vecDirFiles::getCWSubDir( std::string subDir )
{
	wchar_t szDirectory[MAX_PATH];
	//Get Current Directory
	GetCurrentDirectory( sizeof(szDirectory) - 1, szDirectory );
	char dir[MAX_PATH];
	int j = 0;
	//converts a wchar_t string to a char string
	for( int i = 0; szDirectory[i] != '\0'; ++i, ++j )
	{
		dir[i] = (char)szDirectory[i];
	}
	// appends a \ to the end of the current directory path
	dir[j] = '\\';
	j++;
	int k = j;
	// adds the passed subDirectory that the user 
	// wants to access.
	for( int i = j; 1 ; ++i, ++k )
	{
		dir[i] = subDir[ i - j ];
		if( subDir[ i - j ] == '\0' )
			break;
	}
	// makes the array a string by ending it properly
	// with a null character.
	dir[k] = '\0';
	return dir;
}
std::string vecDirFiles::getCWD()
{
	wchar_t szDirectory[MAX_PATH];
	//Get Current Directory
	GetCurrentDirectory( sizeof(szDirectory) - 1, szDirectory );
	char dir[MAX_PATH];
	int j = 0;
	//converts a wchar_t string to a char string
	for( int i = 0; szDirectory[i] != '\0'; ++i, ++j )
	{
		dir[i] = (char)szDirectory[i];
	}
	return dir;
}
void vecDirFiles::setDir( std::string directory )
{
	Dir = convertStringToLPTSTR( directory );
	Dir = catDirEnd( Dir );
	fillVectorWithFiles();
}

LPTSTR vecDirFiles::convertStringToLPTSTR(std::string &str)
{
	unsigned int i;
	LPTSTR p = (LPTSTR) malloc (MAX_PATH);
	for( i = 0; i < str.size(); ++i )
	{
		p[i] = str[i];
	}
	p[i] = '\0';
	return p;
}
//string vecDirFiles::convertLPTSTRToString( LPTSTR str )
//{
//	string p;
//	for( int i = 0; str[i] != '\0'; ++i )
//	{
//		p.push_back( (char)str[i] );
//	}
//	return p;
//}
LPTSTR vecDirFiles::catDirEnd(LPTSTR str)
{
	int i = 0;
	while( str[i] != '\0' )
	{
		++i;
	}
	str[i] = '\\';
	str[i + 1] = '*';
	str[i + 2] = '\0';
	return str;
}
void vecDirFiles::fillVectorWithFiles()
{
	//gets the first file in folder Dir, Dir is located in the Current Working Directory (CWD)
	//so if you passed \Graphics\* it would give you the first file in the (CWD)/Graphics folder
	hFind = FindFirstFile( Dir, &fileInfo );
	if ( hFind == INVALID_HANDLE_VALUE ) 
	{
		//Debugging Code
		std::cout << "function 'fillVectorWithFile()' did not get a valid subfolder passed to it" << std::endl;
		exit(1);
	} 
	else 
	{   
		// List all the files in the directory and push them to sVec
		int i;
		while( FindNextFile( hFind, &fileInfo ) != 0 ) 
		{
			i = 0;
			char p[260] = {NULL};
			//this while loop converts WCHAR[260] to char[260]
			while( fileInfo.cFileName[i] != '\0' )
			{
				p[i] = (char)fileInfo.cFileName[i];
				++i;
			}
			//then we push the file name to our class vector of strings
			sVec.push_back ( p );
		}
	}
}

